package main.java.jdk8;

import java.time.Clock;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

import org.joda.time.DateTime;
import org.joda.time.Months;

/**
 * java新特性：时间的支持
 * 
 * @author Zhzhl
 *
 */
public class ClockTest {
	final static Clock clock = Clock.systemUTC();

	public static void main(String[] args) {
		// test1();
		// test2();
		org.joda.time.LocalDate start = new org.joda.time.LocalDate(2015, 6, 3);
		org.joda.time.LocalDate end = new org.joda.time.LocalDate(2015, 8, 2);
		System.out.println(start.toString());
		System.out.println(start.toString().replace("-", "").substring(0, 6));
		System.out.println(Months.monthsBetween(start, end).getMonths());
	}

	private static void test2() {
		DateTime dateTime = DateTime.now();
		dateTime = dateTime.minusDays(dateTime.dayOfMonth().get() - 1);
		System.out.println(dateTime);

		org.joda.time.LocalDate localDate = org.joda.time.LocalDate.now();
		org.joda.time.LocalDate preMonth = localDate.minusMonths(1);
		org.joda.time.LocalDate firstDateOfPreMonth = new org.joda.time.LocalDate(preMonth.year().get(), preMonth
				.monthOfYear().get(), preMonth.dayOfMonth().getMinimumValue());

		org.joda.time.LocalDate lastDateOfPreMonth = new org.joda.time.LocalDate(preMonth.year().get(), preMonth
				.monthOfYear().get(), preMonth.dayOfMonth().getMaximumValue());

		org.joda.time.LocalDate tempDate = firstDateOfPreMonth;
		while (tempDate.isBefore(lastDateOfPreMonth.plusDays(1))) {
			System.out.println(tempDate);
			tempDate = tempDate.plusDays(1);
		}
	}

	private static void test1() {
		System.out.println(clock.millis());
		System.out.println(clock.instant());

		System.out.println(LocalDate.now(clock));
		System.out.println(LocalTime.now(clock));
		System.out.println(LocalDateTime.now(clock));
		System.out.println(LocalDate.now());
	}
}
